package com.teatrace.action.process;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.commons.util.HibernateUtils;
import com.teatrace.model.process.Consist;
import com.teatrace.model.process.ProcessFlow;
import com.teatrace.model.process.ProcessType;
import com.teatrace.service.process.ProcessService;
import com.teatrace.service.process.impl.ProcessServiceImpl;

public class ProcessFlowManagement extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest servletRequest;
	private ProcessService p = new ProcessServiceImpl();
	private ProcessFlow pf;
	private ProcessType pt;
	
	//增加茶叶加工流程
	//关联加工工序
	public String Add(){
		System.out.println("动作：增加加工流程！");
		System.out.println("加工流程描述为：" + pf.getDescription());
		String[] processtypenames  = servletRequest.getParameterValues("processtypename");
		ProcessType processtype = null;
		//保存加工流程
		p.AddProcessFlow(pf);
		//获取加工流程
		ProcessFlow pf2 = p.GetProcessFlow(pf.getName());
		
		
		Consist consist = new Consist();
		for(int i= 0;i<processtypenames.length;i++){		
			//获取加工工序
			System.out.println("工序名为：" + processtypenames[i]);
			processtype = p.GetProcessType(processtypenames[i]);	
			
			//保存Consist	
			consist.setProcesstype(processtype);
			consist.setProcessflow(pf2);
			consist.setOrdernumber(i+1);	
			
			p.AddConsist(consist);
		}	
		return SUCCESS;  	
	}
	
	//获取主页面需要显示的信息
	public String Show(){
		List<ProcessFlow> pfs = p.GetProcessFlows();
		servletRequest.setAttribute("processflows", pfs);
		return SUCCESS;
	}
	
	//获取修改茶叶加工流程需要显示的信息
	public String ToUpdate(){
		System.out.println("动作：获取修改加工工序页面所需信息！");		
		long ptid = Long.parseLong(servletRequest.getParameter("processflowid"));
		ProcessFlow pf = p.GetProcessFlow(ptid);
		List<ProcessType> pts = p.GetProcessTypes();
		servletRequest.setAttribute("processtypes", pts);
		servletRequest.setAttribute("processflow", pf);
		return SUCCESS;
	}
	
	//删除茶叶加工流程
	public String Delete(){
		long processflowid = Long.parseLong(servletRequest.getParameter("processflowid"));
		p.DeleteProcessFlow(processflowid);
		return SUCCESS;
	}
	
    //修改茶叶加工流程
	//修改=删除后增加
	public String Update(){
		System.out.println("动作：修改加工流程！");
		long processflowid = Long.parseLong(servletRequest.getParameter("processflowid"));
		p.DeleteProcessFlow(processflowid);
		Add();
		return SUCCESS;
	}
	
	
	//获取所有加工工序
	public String GetProcessTypes(){
		System.out.println("动作：获取所有加工工序！");
		List<ProcessType> pts = p.GetProcessTypes();
		servletRequest.setAttribute("processtypes", pts);
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;	
	}
	public ProcessService getP() {
		return p;
	}
	public void setP(ProcessService p) {
		this.p = p;
	}
	public ProcessFlow getPf() {
		return pf;
	}
	public void setPf(ProcessFlow pf) {
		this.pf = pf;
	}
	public ProcessType getPt() {
		return pt;
	}
	public void setPt(ProcessType pt) {
		this.pt = pt;
	}
	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}	
	
}
